package com.yxq.mdt.app.modules.personal.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.yxq.mdt.app.R;
import com.yxq.mdt.app.base.BaseActivity;
import com.yxq.mdt.app.modules.goodsdetail.bean.CollectGoodsBean;
import com.yxq.mdt.app.modules.goodsdetail.model.GoodsModel;
import com.yxq.mdt.app.modules.personal.adapter.MineCircleListAdapter;
import com.yxq.mdt.app.modules.personal.bean.GroupBean;
import com.yxq.mdt.app.modules.personal.model.GroupModel;
import com.yxq.mdt.app.modules.personal.presenter.GroupPresenter;
import com.yxq.mdt.app.modules.personal.presenter.IGroupContract;
import com.yxq.mdt.app.modules.socialcontact.ui.activity.CircleTheHomepageAc;
import com.yxq.mdt.app.net.ApiModel;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * 我的圈子
 */
public class MainPersonalMyCircleAc extends BaseActivity<GroupPresenter> implements IGroupContract.View {


    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    MineCircleListAdapter mineCircleListAdapter;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;

    @Override
    public int getLayoutId() {
        return R.layout.activity_main_personal_circle;
    }

    @Override
    public void initView() {
        mPresenter = new GroupPresenter(this, new GroupModel());
        refreshLayout.setOnRefreshListener(refreshLayout -> {
            page = 1;
            mPresenter.queryUserByAllGroup(page, 30);
        });
        refreshLayout.setOnLoadMoreListener(refreshLayout -> {
            page++;
            mPresenter.queryUserByAllGroup(page, 30);
        });
        mineCircleListAdapter = new MineCircleListAdapter(R.layout.item_mine_circle_layout, null);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(mineCircleListAdapter);
        mineCircleListAdapter.setOnItemClickListener((adapter, view, position) -> {
            Intent intent = new Intent(mActivity, CircleTheHomepageAc.class);
            intent.putExtra("id", mineCircleListAdapter.getItem(position).getId());
            intent.putExtra("CircleName", mineCircleListAdapter.getItem(position).getGroupname());
            startActivity(intent);
        });
        mineCircleListAdapter.setOnItemChildClickListener((adapter, view, position) -> {
            switch (view.getId()) {
                case R.id.item_group_tv_group_join:
                    Intent intent = new Intent(mActivity, CircleTheHomepageAc.class);
                    intent.putExtra("id", mineCircleListAdapter.getItem(position).getId());
                    intent.putExtra("CircleName", mineCircleListAdapter.getItem(position).getGroupname());
                    startActivity(intent);
                    break;
            }
        });
        mineCircleListAdapter.setEmptyView(R.layout.layout_empty_view, recyclerView);
        mineCircleListAdapter.isUseEmpty(false);
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        refreshLayout.autoRefresh();
    }

    @Override
    public void onLoadGroupListSuccess(GroupBean groupBean) {
        refreshLayout.finishLoadMore();
        refreshLayout.finishRefresh();
        if (page == 1) {
            mineCircleListAdapter.setNewData(groupBean.getList());
        } else {
            if (groupBean.getList() != null && groupBean.getList().size() != 0) {
                mineCircleListAdapter.addData(groupBean.getList());
            } else {
                refreshLayout.finishLoadMoreWithNoMoreData();
            }
        }
        mineCircleListAdapter.isUseEmpty(!(mineCircleListAdapter.getData().size() > 0));
    }

    @Override
    public void onLoadGroupListFailed(String msg) {
        refreshLayout.finishLoadMore();
        refreshLayout.finishRefresh();
    }
}
